<?php
/**
 * Date: 2024.8
 * Author: kahn
 * 全局路由配置
 */
namespace config;

class Route
{
    private static Route $_route;

    const JWT_REQUIRED = 0;  //必须验证jwt
    CONST JWT_JUMP = 1;  //跳过验证jwt
    CONST JWT_OPTIONAL = 2;  //非必须验证jwt

    CONST METHOD_GET = 'GET';
    CONST METHOD_POST = 'POST';

    //注册路由
    CONST ROUTE = [
        self::METHOD_GET => [
            '' => ['jwt' => self::JWT_JUMP, 'real' => '', 'sign'=>false, 'common'=>false],
            'index.php' => ['jwt' => self::JWT_JUMP, 'real' => '', 'sign'=>false, 'common'=>false],

            //=============vip==========================================================================================
            'vip/notify/openid' => ['jwt' => self::JWT_JUMP, 'real' => 'vip/notify/openid' ,'sign'=>false, 'common'=>false],
            'vip/notify/wxpay' => ['jwt' => self::JWT_JUMP, 'real' => 'vip/notify/wxpay', 'sign'=>false, 'common'=>false],
            'vip/notify/alipay' => ['jwt' => self::JWT_JUMP, 'real' => 'vip/notify/alipay' ,'sign'=>false, 'common'=>false],
            //=============end==========================================================================================

        ],
        self::METHOD_POST => [
            'jwt' => ['jwt' => self::JWT_JUMP, 'real' => 'index/jwt'], //test

            'sys' => ['jwt' => self::JWT_JUMP, 'real' => 'sys'],
            'sys/version' => ['jwt' => self::JWT_JUMP, 'real' => 'sys/version'],

            //=============uc=============================
            'uc/sms' => ['jwt' => self::JWT_JUMP, 'real' => 'uc/sms/get'],
            'uc/captcha' => ['jwt' => self::JWT_JUMP, 'real' => 'uc/captcha/get'],
            'uc/login' => ['jwt' => self::JWT_JUMP,
                'real' => 'uc/user/login',
                'sign' => true,
                'validate' => [
                    'mobile'=>'require|mobile',
                    'code'=>'require|smscode'
                ],
            ],

            'uc/verify' =>['real' => 'uc/user/verify'],
            'uc/logout' => ['real' => 'uc/user/logout'],
            'uc/logoff' => ['real' => 'uc/user/logoff'],
            'uc/micro/login' =>['jwt' => self::JWT_JUMP, 'real' => 'uc/micro/login'],
            'uc/micro/bind' =>['jwt' => self::JWT_JUMP, 'real' => 'uc/micro/bind'],
            'uc/native/auth' =>['jwt' => self::JWT_JUMP, 'real' => 'uc/native/auth'],
            'uc/profile/get' =>['real' => 'uc/profile/get'],
            'uc/profile/set' => ['jwt' => self::JWT_REQUIRED,'real' => 'uc/profile/set'],
            //=============end==========================================================================================

            //=============vip==========================================================================================
            'vip/order/create' => ['real' => 'vip/order/create'],
            'vip/pay' => ['real' => 'vip/order/pay'],
            'vip/order/query' => ['real' => 'vip/order/query'],
            'vip/iap/receipt' => ['real' => 'vip/iap/receipt'],
            'vip/goods' => ['jwt' => self::JWT_JUMP,'real' => 'vip/goods/list'],
            'vip/sale' => ['jwt' => self::JWT_JUMP, 'real' => 'vip/sale/list'],
            'vip/micro/openid' => ['real' => 'vip/micro/openid'],
            'vip/public/openid' => ['real' => 'vip/public/openid'],
            'vip/is' => ['real' => 'vip/sale/isvip'],
            //=============end==========================================================================================
        ]

    ];

    public static function getRule($method, $route){
        return self::ROUTE[$method][$route]['validate'] ?? [];
    }

    public static function needSign($method, $route){
        return self::ROUTE[$method][$route]['sign'] ?? true;
    }

    public static function jwtType($method, $route)
    {
        return self::ROUTE[$method][$route]['jwt'] ?? self::JWT_REQUIRED;
    }

    public static function needCommon($method, $route)
    {
        return self::ROUTE[$method][$route]['common'] ?? true;
    }


    public function group(): static
    {
        return $this;
    }

    public function method(): static
    {
        return $this;
    }

    public function route(): static
    {
        return $this;
    }

    public function use(): static
    {
        return $this;
    }

    private static function instance(): Route
    {
        if(!isset(self::$_route)){
            self::$_route = new Route();
        }
        return self::$_route;
    }

    public static function __callStatic($name, $arguments){
        return self::instance()->$name(...$arguments);
    }

}


